﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace project_forgotten_tuba
{
    public class GameState
    {
        public Container MainContainer { get; set; }

        public List<Card> AllCards { get; set; }
        public List<Player> AllPlayers { get; set; }

        public Texture2D GameBackground { get; set; }

        public  Dictionary<string, string> dict { get; set; }

        

        public GameState()
        {
           AllCards = new List<Card>();
            AllPlayers = new List<Player>();

            dict = new Dictionary<string, string>();

            List<string> lines = File.ReadLines("deck//deck.txt").ToList();

            
            GameStatic.excel_init("excel_tutorial.xlsx");

            if (GameStatic.excel_loaded == true)
            {
                for (int i = 1; i < 11; i++)
                {

                    string CurrentTag = "A" + i.ToString();
                    string CurrentDef = "B" + i.ToString();

                    string Current2Tag = GameStatic.excel_getValue(CurrentTag);
                    string Current2Def = GameStatic.excel_getValue(CurrentDef);

                    dict.Add(Current2Tag, Current2Def);


                }
                foreach (var pair in dict)
                {
                    Console.WriteLine("{0}, {1}", pair.Key,
                    pair.Value);
                }
            }




            Player TopPlayer = new Player();
            Player BottomPlayer = new Player();

            TopPlayer.MainGame = this;
            BottomPlayer.MainGame = this;

            AllPlayers.Add(TopPlayer);
            AllPlayers.Add(BottomPlayer);


            //use file to texture to load background into game-background property
            GameBackground = GameStatic.FileToTexture("gamebackground.jpg", "cards//");



        }

        public void LoadStringListAsCards(List<string> cards, int playerindex)
        {
            if (AllPlayers[playerindex - 1].DeckLoaded == true) return;
            else AllPlayers[playerindex - 1].DeckLoaded = true;

            foreach (string j in cards)
            {
                if (j != "")
                {

                    Card nextcard = new Card(j);
                    AllCards.Add(nextcard);
                    nextcard.Flipped = true;

                    //if (GameStatic.PlayerIndex == 2)
                    //{
                    //    nextcard.X = GameStatic.WINDOW_WIDTH - GameStatic.CARD_WIDTH / 2;
                    //    nextcard.Y = GameStatic.WINDOW_HEIGHT - GameStatic.CARD_HEIGHT / 2;
                    //}

                    nextcard.X = GameStatic.WINDOW_WIDTH - GameStatic.CARD_WIDTH / 2;
                    nextcard.Y = GameStatic.WINDOW_HEIGHT - GameStatic.CARD_HEIGHT / 2;
                }
            }
        }

        public void Update(float elapsed)
        {

        }


        public void Draw(SpriteBatch spriteBatch)
        {
            //going to draw background here
            spriteBatch.Draw(GameBackground, new Rectangle(0, 0, GameStatic.WINDOW_WIDTH, GameStatic.WINDOW_HEIGHT), Color.White);

            AllPlayers[GameStatic.PlayerIndex - 1].PrivateDraw(spriteBatch);

            foreach (Card c in AllCards.OrderBy(u=>u.ZLevel))
            {
                c.Draw(spriteBatch);
            }
        }



        public void Shuffle(int playerindex)
        {
            //add something to shuffle different places based on player index
           
            Rectangle DeckBox;
            DeckBox = new Rectangle(GameStatic.WINDOW_WIDTH - GameStatic.CARD_WIDTH, GameStatic.WINDOW_HEIGHT - GameStatic.CARD_HEIGHT, 2*GameStatic.CARD_WIDTH, 2*GameStatic.CARD_HEIGHT);


            List<Card> deck;
            deck = AllCards.Where(a => DeckBox.Contains(a.X, a.Y)).ToList();
            Random rnd = new Random();
            List<int> DeckZLevels = deck.Select(a => a.ZLevel).ToList();
            DeckZLevels = DeckZLevels.OrderBy(a => rnd.Next()).ToList();
            for (int i = 0; i < deck.Count; i++)
            {
                deck[i].ZLevel = DeckZLevels[i];
            }


        }


        public void PerformInput(int playerindex, Container.InputType inputtype, int x, int y)
        {
            switch (inputtype)
            {
                case Container.InputType.LeftClick:
                    AllPlayers[playerindex - 1].HandleLeftPress(x, y);
                    break;
                case Container.InputType.LeftRelease:
                    AllPlayers[playerindex - 1].HandleLeftRelease(x, y);
                    break;
                case Container.InputType.LeftDrag:
                    AllPlayers[playerindex - 1].HandleLeftDrag(x, y);
                    break;
                case Container.InputType.Tap:
                    AllPlayers[playerindex - 1].HandleTapCard();
                    break;
                case Container.InputType.Flip:
                    AllPlayers[playerindex - 1].HandleFlipCard();
                    break;
                case Container.InputType.Shuffle:
                    Shuffle(playerindex);
                    break;
            }
        }

    }

}




